"""读取代码到docx中"""
import os
import chardet
from docx import Document

CODE_PATH = '/Users/lichuanzhi/Desktop/waimaobao_v2/data.waimaobao.org_v2.0/Common/'

def list_file(dir_path, ext, files):
    """筛选目录下指定类型的文件"""
    items = os.listdir(dir_path)
    for item in items:
        path = os.path.join('%s/%s' % (dir_path, item))
        if os.path.isdir(path):
            list_file(path, ext, files)
        elif item.endswith(ext):
            files.append(path)

FILES = []
list_file(CODE_PATH, '.cs', FILES)
FILES = FILES[4:5]
print("文件的个数：%d" % len(FILES))
DOCX = Document()
for p in FILES:
    print(p)
    raw = open(p, 'rb').read()
    result = chardet.detect(raw)
    encoding = result['encoding']
    file = open(p, 'r', encoding=encoding)
    i = 0
    for line in file:
        if i > 1980:
            DOCX.add_paragraph(line, style='ListNumber')
        i += 1
    file.close()
    # DOCX.add_page_break()
DOCX.save('/Users/lichuanzhi/Desktop/demo.docx')
